<template>
  <tl-sub-page :render="render" :visible.sync="visible" v-dialogDrag :title="title" :close-on-click-modal="false" class="tl-dialog" width="1200px" @open="open" @close="close">
    <el-tabs v-model="activeTabName" type="card" @tab-click="tabClick">
      <el-tab-pane label="基础信息" name="basic">
        <el-form class="tl-form" ref="form" :model="model" :rules="rules" label-width="150px" :disabled="disableEditSubmit || disableSave">
					<el-row>
						<el-col :span="12">
							<el-form-item label="立项id" prop="projectid" >
								<el-input-number v-model="model.projectid" controls-position="right" :step="1" :min="0" style="width:100%"></el-input-number>
							</el-form-item>
						</el-col>
					</el-row>
					<el-row>
						<el-col :span="24">
							<el-form-item label="任命书标题" prop="appointmenttitle" >
								<el-input v-model="model.appointmenttitle" type="textarea" :rows="4"></el-input>
							</el-form-item>
						</el-col>
					</el-row>
					<el-row>
						<el-col :span="12">
							<el-form-item label="签发人" prop="signername" >
								<el-input v-model="model.signername"></el-input>
							</el-form-item>
						</el-col>
					</el-row>
					<el-row>
						<el-col :span="12">
							<el-form-item label="签发人工号" prop="signercode" >
								<el-input v-model="model.signercode"></el-input>
							</el-form-item>
						</el-col>
						<el-col :span="12">
							<el-form-item label="签发日期" prop="signdate" >
								<el-date-picker v-model="model.signdate" type="date" placeholder="签发日期" value-format="yyyy-MM-dd"></el-date-picker>
							</el-form-item>
						</el-col>
					</el-row>
					<el-row>
						<el-col :span="24">
							<el-form-item label="任命书描述" prop="appointmentdescription" >
								<el-input v-model="model.appointmentdescription" type="textarea" :rows="4"></el-input>
							</el-form-item>
						</el-col>
					</el-row>
					<el-row>
						<el-col :span="12">
							<el-form-item label="项目名称" prop="projectname" >
								<el-input v-model="model.projectname"></el-input>
							</el-form-item>
						</el-col>
						<el-col :span="12">
							<el-form-item label="项目级别" prop="projectlevel" >
								<el-input v-model="model.projectlevel"></el-input>
							</el-form-item>
						</el-col>
					</el-row>
					<el-row>
						<el-col :span="12">
							<el-form-item label="项目经理" prop="projectmanager" >
								<el-input v-model="model.projectmanager"></el-input>
							</el-form-item>
						</el-col>
						<el-col :span="12">
							<el-form-item label="项目经理工号" prop="projectmanagercode" >
								<el-input v-model="model.projectmanagercode"></el-input>
							</el-form-item>
						</el-col>
					</el-row>
					<el-row>
						<el-col :span="12">
							<el-form-item label="Sponsor" prop="sponsor" >
								<el-input v-model="model.sponsor"></el-input>
							</el-form-item>
						</el-col>
						<el-col :span="12">
							<el-form-item label="Sponsor工号" prop="sponsorcode" >
								<el-input v-model="model.sponsorcode"></el-input>
							</el-form-item>
						</el-col>
					</el-row>
					<el-row>
						<el-col :span="12">
							<el-form-item label="Owner" prop="owner" >
								<el-input v-model="model.owner"></el-input>
							</el-form-item>
						</el-col>
						<el-col :span="12">
							<el-form-item label="Owner工号" prop="ownercode" >
								<el-input v-model="model.ownercode"></el-input>
							</el-form-item>
						</el-col>
					</el-row>
					<el-row>
						<el-col :span="12">
							<el-form-item label="顾问" prop="adviser" >
								<el-input v-model="model.adviser"></el-input>
							</el-form-item>
						</el-col>
						<el-col :span="12">
							<el-form-item label="顾问工号" prop="advisercode" >
								<el-input v-model="model.advisercode"></el-input>
							</el-form-item>
						</el-col>
					</el-row>
					<el-row>
						<el-col :span="12">
							<el-form-item label="项目编号" prop="projectcode" >
								<el-input v-model="model.projectcode"></el-input>
							</el-form-item>
						</el-col>
						<el-col :span="12">
							<el-form-item label="系统推送标志" prop="syspush" >
								<el-input v-model="model.syspush"></el-input>
							</el-form-item>
						</el-col>
					</el-row>
					<el-row>
						<el-col :span="12">
							<el-form-item label="公司" prop="orgcode" >
								<el-input v-model="model.orgcode"></el-input>
							</el-form-item>
						</el-col>
						<el-col :span="12">
							<el-form-item label="部门" prop="deptcode" >
								<el-input v-model="model.deptcode"></el-input>
							</el-form-item>
						</el-col>
					</el-row>
					<el-row>
						<el-col :span="12">
							<el-form-item label="申请公司" prop="applyorgname" >
								<el-input v-model="model.applyorgname"></el-input>
							</el-form-item>
						</el-col>
						<el-col :span="12">
							<el-form-item label="申请公司编码" prop="applyorgcode" >
								<el-input v-model="model.applyorgcode"></el-input>
							</el-form-item>
						</el-col>
					</el-row>
					<el-row>
						<el-col :span="12">
							<el-form-item label="申请部门" prop="applydeptname" >
								<el-input v-model="model.applydeptname"></el-input>
							</el-form-item>
						</el-col>
						<el-col :span="12">
							<el-form-item label="申请部门编码" prop="applydeptcode" >
								<el-input v-model="model.applydeptcode"></el-input>
							</el-form-item>
						</el-col>
					</el-row>
					<el-row>
						<el-col :span="12">
							<el-form-item label="申请人姓名" prop="applyusername"  :rules="rules.validateRequire">
								<tl-ref-user v-model="model.applyusername" :initDisplayData.sync="model.dispapplyusername" dataType="code" :singleSelect="true"></tl-ref-user>
							</el-form-item>
						</el-col>
						<el-col :span="12">
							<el-form-item label="申请人工号" prop="applyusercode" >
								<el-input v-model="model.applyusercode"></el-input>
							</el-form-item>
						</el-col>
					</el-row>
					<el-row>
						<el-col :span="12">
							<el-form-item label="申请人工号2" prop="usercode" >
								<el-input v-model="model.usercode"></el-input>
							</el-form-item>
						</el-col>
						<el-col :span="12">
							<el-form-item label="申请人姓名2" prop="username" >
								<el-input v-model="model.username"></el-input>
							</el-form-item>
						</el-col>
					</el-row>
					<el-row>
						<el-col :span="12">
							<el-form-item label="提交时间" prop="submitdate" >
								<el-date-picker v-model="model.submitdate" type="date" placeholder="提交时间" value-format="yyyy-MM-dd"></el-date-picker>
							</el-form-item>
						</el-col>
					</el-row>
					<el-row>
						<el-col :span="12">
							<el-form-item label="审批流程" prop="flowid" >
								<el-select v-model="model.flowid" filterable clearable>
									<el-option v-for="item in codeModel.flowidList" :key="item.id" :label="item.name" :value="parseInt(item.id)" />
								</el-select>
							</el-form-item>
						</el-col>
						<el-col :span="12">
							<el-form-item label="状态" prop="state" >
								<el-select v-model="model.state" filterable clearable>
									<el-option v-for="item in codeModel.stateList" :key="item.code" :label="item.name" :value="parseInt(item.code)" />
								</el-select>
							</el-form-item>
						</el-col>
					</el-row>
					<el-row>
						<el-col :span="24">
							<el-form-item label="备注" prop="description" >
								<el-input v-model="model.description" type="textarea" :rows="4"></el-input>
							</el-form-item>
						</el-col>
					</el-row>
					<el-row>
						<el-col :span="12">
							<el-form-item label="申请日期" prop="applydate" >
								<el-date-picker v-model="model.applydate" type="date" placeholder="申请日期" value-format="yyyy-MM-dd"></el-date-picker>
							</el-form-item>
						</el-col>
					</el-row>
					<el-row>
						<el-col :span="24">
							<el-form-item label="变更原因" prop="changereason" >
								<el-input v-model="model.changereason" type="textarea" :rows="4"></el-input>
							</el-form-item>
						</el-col>
					</el-row>
					<el-row>
						<el-col :span="12">
							<el-form-item label="版本号" prop="version" >
								<el-input-number v-model="model.version" controls-position="right" :step="1" :min="0" style="width:100%"></el-input-number>
							</el-form-item>
						</el-col>
					</el-row>
					<el-row>
						<el-col :span="12">
							<el-form-item label="排序码" prop="sortcode">
								<el-input-number v-model="model.sortcode" controls-position="right" :step="1" :min="0" style="width:100%"></el-input-number>
							</el-form-item>
						</el-col>
						<el-col :span="12">
							<el-form-item label="有效" prop="enabled">
								<el-radio-group v-model="model.enabled">
									<el-radio :label="parseInt(item.code)" v-for="item in codeModel.enabledList" :key="item.code">{{ item.name }}</el-radio>
								</el-radio-group>
							</el-form-item>
						</el-col>
					</el-row>
        </el-form>
      </el-tab-pane>
      <el-tab-pane label="明细" name="line">
        <!-- <tl-line ref="line" :parentId="model.id" :editable="!disableSave"></tl-line> -->
      </el-tab-pane>
    </el-tabs>
    <tl-approval-history ref="history" sourceType="GpmProjectAppointment" :sourceId="model.id" />
    <div slot="footer" class="dialog-footer">
      <el-button :type="disableEditSubmit === false ? 'text' : 'primary'" @click="doCancelEdit">取消</el-button>
      <el-button type="primary" @click="doSubmitEdit" :loading="saveLoading" v-if="!disableEditSubmit" :disabled="disableSave"><svg-icon icon-class="save" />保存</el-button>
      <el-button type="primary" @click="doSubmitApproval" :loading="submitLoading" v-if="!disableEditSubmit" :disabled="disableApproval"><svg-icon icon-class="send" />提交审批</el-button>
      <tl-attachment sourceType="GpmProjectAppointment" :sourceId="model.id" :disabled="disableAttachment" :editable="stateEdit"></tl-attachment>
      <el-button type="primary" @click="$refs.history.visible = true" :disabled="disableApprovalHistory"><svg-icon icon-class="list" />审批历史</el-button>
      <!-- <el-button type="primary" @click="doDelApproval" :loading="delApprovalLoading" v-if="!disableEditSubmit" :disabled="disableDelApproval"><svg-icon icon-class="undo" />取消审批</el-button> -->
    </div>
  </tl-sub-page>
</template>

<script>
/** mixins */
import { mixin_approval_edit_edit } from '../../../mixins/mixin_approval_edit'
/** 子组件 */
// import tlLine from '../sdProjectAppointmentLine/indexFrame'
/** 工具类 */
import { putAction, getAction, postAction, deleteAction } from '@tapui/utils/http'
/** VUE配置 */
export default {
  mixins: [mixin_approval_edit_edit],
//   components: { tlLine },
  data() {
    return {
      moduleTitle: 'SD项目任命',
      urlPrefix: '/sd/projectAppointment/',
      url: {
        // 额外的url
      },
      activeTabName: 'basic',
      isRefreshLine: false
    }
  },
  methods: {
    assignDefaultValue() {
      if (this.isAdd) {
        if (this.codeModel.flowidList && this.codeModel.flowidList.length > 0) {
          this.$set(this.model, 'flowid', parseInt(this.codeModel.flowidList[0].id))
        }
        this.$set(this.model, 'orgcode', this.org.code)
        this.$set(this.model, 'orgname', this.org.name)
        this.$set(this.model, 'deptcode', this.dept.code)
        this.$set(this.model, 'deptname', this.dept.fullname)
        this.$set(this.model, 'usercode', this.user.code)
        this.$set(this.model, 'username', this.user.name)
      }
    },
    loaded() {
      // 页面加载完毕执行
      this.activeTabName = 'basic'
      this.isRefreshLine = false
      if (!this.isAdd && this.model.docno) {
        this.title = this.title + `（单据编号：${this.model.docno}）`
      }
    },
    tabClick(tab) {
      if (tab.name === 'line') {
        if (!this.model.id) {
          this.$nextTick((_) => {
            this.activeTabName = 'basic'
          })
          this.$tap
            .confirm('当前数据未保存，是否立即保存数据？')
            .then((_) => {
              this.doSubmitEdit()
            })
            .catch((_) => {})
        } else if (!this.isRefreshLine) {
          this.$refs.line.doQuery()
          this.isRefreshLine = true
        }
        this.$refs.line.initPage()
      }
    },
    doMultiApproval() {
      let that = this
      that.submitLoading = true
      this.$tap.confirm('确定提交审批吗？提交后，单据内容不可修改！').then((_) => {
        this.$refs.form.validate(async (valid) => {
          if (valid) {
            let saveresult = await that.addBase()
            if (saveresult) {
              this.saveLoading = true
              putAction(this.url.submit, this.model)
                .then((res) => {
                  if (res.success) {
                    that.queryData(that.model.id).then((_) => {
                      that.loaded()
                      that.saveLoading = false
                      that.submitLoading = false
                    })
                    that.$tap.alert('提交审批成功', 'success')
                  } else {
                    that.$tap.alert(res.message, 'warning')
                    that.saveLoading = false
                    that.submitLoading = false
                  }
                })
                .catch((err) => {
                  that.saveLoading = false
                  that.submitLoading = false
                })
            }
          } else {
            that.submitLoading = false
            that.$tap.alert('按要求填写表单数据项，包括必填项，数据格式等')
          }
        })
      })
    }
  }
}
</script>

